package wk.demo.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.luoye.bzijkplayer.R;

import java.io.File;

import wk.demo.adapter.FileListAdapter;
import wk.demo.bean.DirFiles;
import wk.demo.bean.MarginDecoration;
import wk.demo.bean.PathCursorLoader;
import wk.demo.callback.FileSelectCallback;


public class FileListFragment extends Fragment   {
    private static final String ARG_PATH = "path";

    private TextView mPathView;
    private RecyclerView fileListRecyclerView;
    private FileListAdapter mAdapter;
    private String mPath;
    private FileSelectCallback fileSelectCallback ;

    public static FileListFragment newInstance(String path ,FileSelectCallback fileSelectCallback) {
        FileListFragment f = new FileListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PATH, path);
        f.setArguments(args);
        f.fileSelectCallback =fileSelectCallback ;
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_file_list, container, false);
        mPathView = view.findViewById(R.id.path_view);
        fileListRecyclerView = view.findViewById(R.id.fileListRecyclerView);
        fileListRecyclerView.setHasFixedSize(true);
        fileListRecyclerView.setLayoutManager(new GridLayoutManager( getActivity(), 1));
        fileListRecyclerView.addItemDecoration(new MarginDecoration(getActivity()));
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mPath = bundle.getString(ARG_PATH);
            mPathView.setText(mPath);
        }

        mAdapter = new FileListAdapter(getActivity(), new FileListAdapter.OnItemClickCallBack() {
            @Override
            public void onClick(File file  ) {
                fileSelectCallback.onSelect( file );
            }
        });
        fileListRecyclerView.setAdapter(mAdapter);
        getLoaderManager().initLoader(10086, null, loaderCallbacks);
    }


    private  LoaderManager.LoaderCallbacks<DirFiles> loaderCallbacks = new LoaderManager.LoaderCallbacks<DirFiles>() {
        @Override
        public Loader<DirFiles> onCreateLoader(int id, Bundle args) {
            return new PathCursorLoader(getActivity(), mPath);
        }

        @Override
        public void onLoadFinished( Loader<DirFiles> loader, DirFiles data) {
            mAdapter.updateData(data);
        }

        @Override
        public void onLoaderReset(Loader<DirFiles> loader) {
            // to do something ...

        }
    };




}
